<?php
class omeanalysts_mdl_ome_expressreconciliation extends dbeav_model{

    //var $has_export_cnf = true;

    var $export_name = '快递对账报表';

    public function table_name($real=false){
        $table_name = 'ome_delivery';
        if($real){
            return kernel::database()->prefix.$table_name;
        }else{
            return $table_name;
        }
    }

    public function count($filter=null){

        $where = $this->_filter($filter); 

        $sql = "SELECT  count(*) as _count 
                           FROM  sdb_ome_orders os
                           INNER JOIN sdb_ome_delivery_order odo ON ( odo.order_id = os.order_id )
                           INNER JOIN sdb_ome_delivery od ON (  od.delivery_id = odo.delivery_id )
                           INNER JOIN sdb_wms_delivery e ON (e.outer_delivery_bn = od.delivery_bn)
                           INNER JOIN sdb_ome_dly_corp opc ON (e.logi_name = opc.name)
                           INNER JOIN sdb_ome_branch sob ON (sob.branch_id = e.branch_id)
                          WHERE ".$where." ";
       

        $row = $this->db->select($sql);
        return intval($row[0]['_count']);
    }

    public function getlist($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
  

        if(!$filter['time_from'] && !$filter['time_to']){
            return false;
        }else{

            $where = $this->_filter($filter); 
            //var_dump($where);
            $sql = "SELECT  e.logi_name,od.logi_no,e.net_weight/1000 net_weight,sob.`name` AS branch,os.ship_area,os.ship_addr,e.delivery_time
                         FROM  sdb_ome_orders os
                         INNER JOIN sdb_ome_delivery_order odo ON ( odo.order_id = os.order_id )
                         INNER JOIN sdb_ome_delivery od ON (  od.delivery_id = odo.delivery_id )
                         INNER JOIN sdb_wms_delivery e ON (e.outer_delivery_bn = od.delivery_bn)
                         INNER JOIN sdb_ome_dly_corp opc ON (e.logi_name = opc.name)
                         INNER JOIN sdb_ome_branch sob ON (sob.branch_id = e.branch_id)
                        WHERE ".$where." ";
             
            $rows = $this->db->selectLimit($sql,$limit,$offset);
            
//
            return $rows;
        }
        
    }
    
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'order_bn'=>app::get('base')->_('订单号'),
        );
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null)
    {


        $where = array(1);
       if(empty($filter['time_from']))
        {
          $where[]=' 1=2 ';
        }
        if(isset($filter['time_from']) && $filter['time_from']){
            $where[] = ' e.delivery_time >='.strtotime($filter['time_from']);
        }
        if(isset($filter['time_to']) && $filter['time_to']){
            $where[] = ' e.delivery_time <'.(strtotime($filter['time_to'])+86399);
        }
       
        if(isset($filter['logi_name']) && $filter['logi_name']){
            $where[]= ' e.logi_name  =\''.addslashes($filter['logi_name']).'\'';
        }else {
          $where[]= ' opc.AxTyoe != \'200000330\'';
        }


        if(isset($filter['branch']) && $filter['branch']){
            $where[] = ' sob.name =\''.addslashes($filter['branch']).'\'';
        }
        if(isset($filter['logi_no']) && $filter['logi_no']){
            $where[] = ' od.logi_no =\''.addslashes($filter['logi_no']).'\'';
        }
        if(isset($filter['net_weightdy']) && $filter['net_weightdy']){
            $where[] = ' e.net_weight/1000 >= \''.addslashes($filter['net_weightdy']).'\'';
        }
        if(isset($filter['net_weightxy']) && $filter['net_weightxy']){
            $where[] = ' e.net_weight/1000 <= \''.addslashes($filter['net_weightxy']).'\'';
        }
        

        return implode($where,' AND ');
    }

    public function get_schema(){
        $schema = array (
          'columns' => array (
                 'logi_name'=>
                array(
                    'type'=>'varchar(32)',
                    'label'=>'快递公司',
                     'in_list' => true,
                  'default_in_list' => true,
                  'width' => '80',
                ),
                'logi_no' =>
                array (
                  'type' => 'varchar(32)',
                  'required' => true,
                  'label' => '快递单号',
                  'editable' => false,
                  'width' =>140,
                  'filtertype' => 'yes',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '80',
                ),
                'net_weight' =>
                array (
                  'type' => 'number',
                  'label' => '重量',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                  'width' => '80',
                ),
                'branch' =>
                array(
                    'type' => 'varchar(50)',
                    'required' => true,
                    'label' => '仓库',
                    'is_title' => true,
                    'width' => 135,
                    'searchtype' => 'has',
                    'editable' => false,
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'in_list' => true,
                    'default_in_list' => true,
                  'width' => '60',
                ),
                'ship_area' =>
                array (
                  'type' => 'varchar(100)',
                  'required' => true,
                  'label' => '收货地区',
                  'comment' => '收货地区',
                  'editable' => false,
                  'width' =>140,
                  'searchtype' => 'has',
                  'filtertype' => 'yes',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
                'ship_addr' =>
                array (
                  'type' => 'varchar(100)',
                  'required' => true,
                  'label' => '发货地址',
                  'comment' => '发货地址',
                  'editable' => false,
                  'width' =>140,
                  'searchtype' => 'has',
                  'filtertype' => 'yes',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
                'delivery_time' =>
                array (
                  'type' => 'time',
                  'label' => '发货完成时间',
                  'comment' => '发货时间',
                  'editable' => false,
                  'in_list' => true,
                  'width' => '120',
                ),
            ),
            'idColumn' => 'name',
            'in_list' => array (
                0 => 'logi_name',
                1 => 'logi_no',
                2 => 'net_weight',
                3 => 'branch',
                4 => 'ship_area',
                5 => 'ship_addr',
                6 => 'delivery_time',
                
                
            ),
            'default_in_list' => array (
                0 => 'logi_name',
                1 => 'logi_no',
                2 => 'net_weight',
                3 => 'branch',
                4 => 'ship_area',
                5 => 'ship_addr',
                6 => 'delivery_time',
            ),
        );
        return $schema;
    }

    /**
     * 获得日志类型(non-PHPdoc)
     * @see dbeav_model::getLogType()
     */
    public function getLogType($logParams) {
        $type = $logParams['type'];
        $logType = 'none';
        if ($type == 'export') {
            $logType = $this->exportLogType($logParams);
        }
        elseif ($type == 'import') {
            $logType = $this->importLogType($logParams);
        }
        return $logType;
    }
    /**
     * 导出日志类型
     * @param Array $logParams 日志参数
     */
    public function exportLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_export';
        return $type;
    }
    /**
     * 导入操作日志类型
     * @param Array $logParams 日志参数
     */
    public function importLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_import';
        return $type;
    }

    function io_title( $filter, $ioType='csv' ){

        switch( $filter ){
            case 'delivery':
                $this->oSchema['csv'][$filter] = array(
                    '*:快递公司' => 'logi_name',
                    '*:快递单号' => 'logi_no',
                    '*:重量' => 'net_weight',
                    '*:仓库' => 'branch',
                    '*:收货地区' => 'ship_area',
                    '*:发货地址' => 'ship_addr',
                    '*:发货完成时间' => 'delivery_time',
                    
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType][$filter] );
        return $this->ioTitle[$ioType][$filter];
    }

    //根据查询条件获取导出数据
    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end){
          //根据选择的字段定义导出的第一行标题
          if($curr_sheet == 1){

              $title = array();
              foreach($this->io_title('delivery') as $k => $v ){
                  $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
              }

              $data['content']['main'][0] = '"'.implode('","',$title).'"';
          }

          $where = $this->_filter($filter);
          $sql = "SELECT  e.logi_name,od.logi_no,e.net_weight/1000 net_weight,sob.`name` AS branch,os.ship_area,os.ship_addr,e.delivery_time
                         FROM  sdb_ome_orders os
                         INNER JOIN sdb_ome_delivery_order odo ON ( odo.order_id = os.order_id )
                         INNER JOIN sdb_ome_delivery od ON (  od.delivery_id = odo.delivery_id )
                         INNER JOIN sdb_wms_delivery e ON (e.outer_delivery_bn = od.delivery_bn)
                         INNER JOIN sdb_ome_dly_corp opc ON (e.logi_name = opc.name)
                         INNER JOIN sdb_ome_branch sob ON (sob.branch_id = e.branch_id)
                        WHERE ".$where." ";
          
            $omedelivery = $this->db->selectLimit($sql,$end,$start);
            
            foreach ($omedelivery as $k => $aFilter) {
                $exptmp_data = array();
                foreach ($aFilter as $col => $val) {
                    $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                    $exptmp_data[] = $aFilter[$col];
                }

                $data['content']['main'][] = implode(',', $exptmp_data);
            }
            return $data;
        
    }
}
